package com.itheima.a04udpdemo3;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;

public class ReceiveMessageDemo2 {
    public static void main(String[] args) throws IOException {
        //创建MulticastSocket对象
        MulticastSocket ms = new MulticastSocket(10086);

        //加入指定的组
        InetAddress address = InetAddress.getByName("224.0.0.255");
        ms.joinGroup(address);

        //创建包来接收数据
        byte[] bytes = new byte[1024];
        DatagramPacket dp = new DatagramPacket(bytes, bytes.length);

        while (true) {
            //接收数据
            ms.receive(dp);

            //解析数据
            System.out.println(dp.getAddress().getHostAddress()
                    +"的"+dp.getPort()+"端口发来了"+new String(dp.getData()));
        }
    }
}
